package com.ruoyi.project.product.size.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.size.domain.Size;
import com.ruoyi.project.product.size.service.SizeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/15
 * @Description: com.ruoyi.project.product.size.controller
 * @version: 2.0
 */
@Controller
@RequestMapping("/product/size")
public class SizeController extends BaseController {

    @Autowired
    private SizeService sizeService;

    //声明资源地址的公共前缀
    private String prefix = "product/size";

    //声明单元方法:跳转产品规格页面
    @RequiresPermissions("product:size:view")
    @GetMapping()
    public String size(){
        return prefix+"/size";
    }

    @RequiresPermissions("product:size:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Size size)
    {
        startPage();
        List<Size> list = sizeService.selectSizeList(size);
        return getDataTable(list);
    }

    /**
     * 新增产品规格
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品规格
     */
    @RequiresPermissions("product:size:add")
    @Log(title = "产品规格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Size size)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(sizeService.checkSizeNameUnique(size)))
        {
            return error("新增产品规格'" + size.getSizeName() + "'失败，产品规格名称已存在");
        }
        return toAjax(sizeService.insertSize(size));
    }

    /**
     * 校验产品规格名称
     */
    @PostMapping("/checkSizeNameUnique")
    @ResponseBody
    public String checkSizeNameUnique(Size size)
    {
        return sizeService.checkSizeNameUnique(size);
    }

    /**
     * 修改产品规格
     */
    @GetMapping("/edit/{sizeId}")
    public String edit(@PathVariable("sizeId") Long sizeId, ModelMap mmap)
    {
        mmap.put("size", sizeService.selectSizeById(sizeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存产品规格
     */
    @RequiresPermissions("product:size:edit")
    @Log(title = "产品规格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Size size)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(sizeService.checkSizeNameUnique(size)))
        {
            return error("修改产品规格'" + size.getSizeName() + "'失败，产品规格名称已存在");
        }
        return toAjax(sizeService.updateSize(size));
    }

    /**
     * 删除产品规格
     */
    @RequiresPermissions("product:size:remove")
    @Log(title = "产品规格管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(sizeService.deleteSizeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
